/*
    Copyright (C) 2010  Ahti Legonkov

    This file is part of j+-.

    j+- is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    j+- is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package jpm;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * This class contains several overloads of static function invoke. This makes
 * deciding whether to try to call __destructor__ on object or not to the java
 * compiler and code that generates java code is simpler as a result.
 * @author Ahti Legonkov
 */
public final class Destructor
{
    /** There is no need to create objects of this class. Explicitly forbid
     * that by making the constructor private.
     */
    private Destructor() { }

    //
    // Destructors for primitive types
    //
    
    public static void invoke(boolean o) { }
    public static void invoke(byte o) { }
    public static void invoke(char o) { }
    public static void invoke(short o) { }
    public static void invoke(int o) { }
    public static void invoke(float o) { }
    public static void invoke(double o) { }
    public static void invoke(long o) { }

    /** Destructor for non IDestructible objects. */
    public static void invoke(Object o) { }

    /**
     * Invokes destructor of an object using virtual dispatch.
     * @param o reference to an object to invoke destructor for.
     */
    public static void invoke(IDestructible o)
    {
        assert(o != null);
        o.__destructor__();
    }

    /**
     * Invoke base class destructor without involving virtual dispatch.
     * @param <T> base class type
     * @param o reference to an object of type X that extends type T
     * @param c class info of type T.
     */
    public static <T> void invoke(T o, Class<T> c)
    {
        assert(o != null);
        try
        {
            Class<IDestructible> cc = IDestructible.class;
            if (cc.isAssignableFrom(c))
            {
                Method m = c.getDeclaredMethod("__destructor__", c);
                int mod = m.getModifiers();
                if ((mod & Modifier.STATIC) == Modifier.STATIC)
                    m.invoke(c, o);
            }
        }
        catch (IllegalAccessException ex)
        {
            System.err.println("__destructor__ not accessible!");
            assert(false);
        }
        catch (InvocationTargetException ex)
        {
        }
        catch (NoSuchMethodException ex)
        {
            System.err.println("There should be a __destructor__ method!");
            assert(false);
        }
    }
}
